/*
 *  ======== compile ========
 */
function compile(container, target, goalFile, file, res)
{
    if (file.base.match(/^package\//)) {
        return;
    }

    if (file.srcPrefix == 'package/cfg/') {
        _compileCfg(container, target, goalFile, file, res);
        return;
    }

    if (!container.match(RegExp('\\.a' + target.suffix + '$'))) {
        return;
    }

    var isCpp = file.srcSuffix == '.cpp';

    var carr = res.cmds.split('\n');
    
    var idir = 'package/blas.' + target.suffix;
    var ifile = idir + '/' + file.base + (isCpp ? '.ii' : '.i');

    var mkdir = '$(MKDIR) -p ' + idir;
    var msg = "$(MSG) '===> blas module compile'";

    var cmdE = carr[0];
    var cmdC = carr[0];
    
    cmdE = cmdE.replace(' -c ', ' -E ');
    cmdE = cmdE.replace(' -o $@ ', ' -o ' + ifile + ' '); 
    cmdE = cmdE.replace(' -MD -MF $@.dep ', ' ');

    res.cmds = msg + '\n' + mkdir + '\n' + cmdE + '\n' + cmdC + '\n' + carr[1] + '\n';
}

/*
 *  ======== link ========
 */
function link(container, cfgBase, prog, objList, linkArgs, res)
{
    if (this.libsOnly) return;

    var ifile = cfgBase + '.i';
    var sfile = cfgBase + '.opt';
    var bfile = cfgBase + '.blas';
    var ofile = cfgBase + '.o' + prog.target.suffix;

    var msg = "$(MSG) '===> blas config link'";
    var gcc = res.cmds.substring(0, res.cmds.indexOf(' '));

    var cmdS = gcc + ' -S -O3 -combine -fwhole-program -ffunction-sections -fdata-sections -o ' + sfile + ' ' + ifile + ' `$(CAT) ./' + bfile + '`';
    var cmdC = gcc + ' -c -x assembler -o ' + ofile + ' ' + sfile;

    res.cmds = msg + '\n' + cmdS + '\n' + cmdC + '\n' + res.cmds;
}

/*
 *  ======== getGenTab ========
 */
function getGenTab(progOrAssem, cfgDir, configName)
{
    return [{template: 'sysbios/targets/gnu/blas/genBlas.xdt', file: cfgDir + configName + '.blas'}];
}

/*
 *  ======== _compileCfg ========
 */
function _compileCfg(container, target, goalFile, file, res)
{
    var ifile = file.srcPrefix + file.base + '.i';

    var msg = "$(MSG) '===> blas config compile'";

    var carr = res.cmds.split('\n');
    var cmdE = carr[0];
    var cmdC = carr[0];

    cmdE = cmdE.replace(' -c ', ' -E ');
    cmdE = cmdE.replace(' -o $@ ', ' -o ' + ifile + ' '); 
    cmdE = cmdE.replace(' -MD -MF $@.dep ', ' ');

    res.cmds = msg + '\n' + cmdE + '\n' + cmdC + '\n' + carr[1] + '\n';
}


